package com.tongzhou.tongzhou.quot.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IBasePaperDAO;
import com.tongzhou.tongzhou.quot.model.BasePaper;
import com.tongzhou.tongzhou.quot.model.Paper;
import com.tongzhou.tongzhou.quot.model.SearchBasePaperCondition;
import com.tongzhou.util.Util;

public class BasePaperDAO implements IBasePaperDAO{

	@Override
	public int addBasePaper(BasePaper basePaper) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into base_paper(supplier,dl,sckzpjz,hxhy,npqd,yzgg,paper_paper_id) ");
		sql.append("values('"+Util.changeToObj(basePaper.getSupplier())+"',");
		sql.append("'"+Util.changeToObj(basePaper.getDl())+"',");
		sql.append("'"+Util.changeToObj(basePaper.getSckzpjz())+"',");
		sql.append("'"+Util.changeToObj(basePaper.getHxhy())+"',");
		
		sql.append("'"+Util.changeToObj(basePaper.getNpqd())+"',");
		sql.append("'"+Util.changeToObj(basePaper.getYzgg())+"',");
		sql.append("'"+basePaper.getPaper_paper_id()+"')");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deleteBasePaper(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from base_paper where id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public BasePaper getBasePaperById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,supplier,dl,sckzpjz,hxhy,npqd,yzgg,paper_paper_id ");
		sql.append(" from base_paper ");
		sql.append(" where id='"+id+"'");
		BasePaper BasePaper=DBUtil.query(sql.toString(), new BeanHandler<BasePaper>(BasePaper.class));
		return BasePaper;
	}

	@Override
	public List<BasePaper> listBasePaper(SearchBasePaperCondition condition)
			throws DBException {
		List<BasePaper> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from base_paper ");
		sql.append(" where 1=1 ");
		sql.append(" and paper_paper_id='"+condition.getPaper_paper_id()+"'");
		sql.append(sqlCondition);
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,supplier,dl,sckzpjz,hxhy,npqd,yzgg,paper_paper_id ");
		sql.append(" from base_paper ");
		sql.append(" where 1=1 ");
		sql.append(" and paper_paper_id='"+condition.getPaper_paper_id()+"'");
		sql.append(sqlCondition);
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<BasePaper>(BasePaper.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updateBasePaper(BasePaper basePaper) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update base_paper set ");
		sql.append("supplier='"+Util.changeToObj(basePaper.getSupplier())+"',");
		sql.append("dl='"+Util.changeToObj(basePaper.getDl())+"',");
		sql.append("sckzpjz='"+Util.changeToObj(basePaper.getSckzpjz())+"',");
		sql.append("hxhy='"+Util.changeToObj(basePaper.getHxhy())+"',");
		sql.append("npqd='"+Util.changeToObj(basePaper.getNpqd())+"',");
		sql.append("yzgg='"+Util.changeToObj(basePaper.getYzgg())+"' ");
		sql.append("where id='"+basePaper.getId()+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public List<BasePaper> getBasePaperList(String paper_name) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  bp.id,bp.supplier,bp.dl,bp.sckzpjz,bp.hxhy,bp.npqd,bp.yzgg,bp.paper_paper_id ");
		sql.append(" from base_paper bp,paper_paper pp ");
		sql.append(" where 1=1 ");
		sql.append(" and bp.paper_paper_id=pp.id ");
		sql.append(" and pp.paper_name='"+paper_name+"'");
		List<BasePaper> result=DBUtil.query(sql.toString(),new BeanListHandler<BasePaper>(BasePaper.class));
		return result;
	}
}
